package at.fpmedv.eclipse.helma.console;

import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleFactory;
import org.eclipse.ui.console.IConsoleManager;

import at.fpmedv.eclipse.helma.core.HelmaCorePlugin;

/**
 * Factory for our helma Console View.
 * @author fpm
 *
 */
public class HelmaConsoleFactory implements IConsoleFactory {


	/**
	 * called when the view should be shown
	 */
	public void openConsole() {
		showConsole();
	}
	
	/**
	 * shows the helma logging view in the console view
	 */
	public static void showConsole() {
		HelmaLoggingConsole console = HelmaLoggingConsole.getInstance();
		if (console != null) {
			IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
			IConsole[] existing = manager.getConsoles();
			boolean exists = false;
			for (int i = 0; i < existing.length; ++i) {
				if(console == existing[i])
					exists = true;
			}
			if(! exists)
				manager.addConsoles(new IConsole[] {console});
			manager.showConsoleView(console);
		} else {
			HelmaCorePlugin.getDefault().log("Error getting console");
		}
	}
}
